class AcePlayerController extends PlayerController;

var AcePawn MyAcePawn;
var AcePlayer1 P1;
var AcePlayer2 P2;

simulated function PostBeginPlay()
{
	local AcePlayer1 A;
	local AcePlayer2 A2;
	
	super.PostBeginPlay();

	foreach AllActors(class'AcePlayer1', A)
	{
		P1 = A;
	}
	
	foreach AllActors(class'AcePlayer2', A2)
	{
		P2 = A2;
	}
}

event Destroyed()
{
	super.Destroyed();
}

exec function DoTestStuff()
{
	if( MyAcePawn.LinkPawn == none )
		return;

	if( MyAcePawn.LinkType == eLinkType_VARIABLE )
	{
		SetPhysics( PHYS_Flying );
		MyAcePawn.LinkType = eLinkType_FIX;
		MyAcePawn.LinkCurrentDistanceMax = VSize2D( MyAcePawn.Location-MyAcePawn.LinkPawn.Location );
	}
	else if( MyAcePawn.LinkType == eLinkType_FIX )
	{
		SetPhysics( PHYS_Walking );
		MyAcePawn.LinkType = eLinkType_VARIABLE;
	}
}

function PlayerTick( float DeltaTime )
{
	local float movespeed;
	local Vector moveVec;
	super.PlayerTick( DeltaTime );

	movespeed = 5.0f;

	//Player 1 movement
	if(AcePlayerInput(PlayerInput) != none)
	{
		if(AcePlayerInput(PlayerInput).LastKey == 'I')
		{
			moveVec = vect(0,0,0);
			moveVec.X = movespeed;
			P1.Velocity += moveVec;
		}
		if(AcePlayerInput(PlayerInput).LastKey == 'K')
		{
			moveVec = vect(0,0,0);
			moveVec.X = -movespeed;
			P1.Velocity += moveVec;
		}
		if(AcePlayerInput(PlayerInput).LastKey == 'L')
		{
			moveVec = vect(0,0,0);
			moveVec.Y = movespeed;
			P1.Velocity += moveVec;
		}
		if(AcePlayerInput(PlayerInput).LastKey == 'J')
		{
			moveVec = vect(0,0,0);
			moveVec.Y = -movespeed;
			P1.Velocity += moveVec;
		}

		//Player 2 movement
		if(AcePlayerInput(PlayerInput).LastKey == 'G')
		{
			moveVec = vect(0,0,0);
			moveVec.X = movespeed;
			P2.Velocity += moveVec;
		}
		if(AcePlayerInput(PlayerInput).LastKey == 'B')
		{
			moveVec = vect(0,0,0);
			moveVec.X = -movespeed;
			P2.Velocity += moveVec;
		}
		if(AcePlayerInput(PlayerInput).LastKey == 'N')
		{
			moveVec = vect(0,0,0);
			moveVec.Y = movespeed;
			P2.Velocity += moveVec;
		}
		if(AcePlayerInput(PlayerInput).LastKey == 'V')
		{
			moveVec = vect(0,0,0);
			moveVec.Y = -movespeed;
			P2.Velocity += moveVec;
		}
	}
}

function Possess( Pawn aPawn, bool bVehicleTransition )
{
	Super.Possess( aPawn, bVehicleTransition );

	MyAcePawn = AcePawn( aPawn );
	if( MyAcePawn == none )
		`warn( "Wrong pawn set for" @ Name @ "(pawn" @ aPawn.Name @ ")" );
}

state PlayerWalking
{
	ignores SeePlayer, HearNoise, Bump;

	function UpdateRotation( float DeltaTime )
	{
		local Rotator new_rotation;

		if( Pawn == none )
		{
			super.UpdateRotation( DeltaTime );
		}
		else
		{
			if( !IsZero(MyAcePawn.Velocity) )
			{
				new_rotation = Rotator( MyAcePawn.Velocity*Vect(1,1,0) );
				//MyAcePawn.SetDesiredRotation( new_rotation, false, true, 0.1f, false );
				MyAcePawn.SetRotation( new_rotation );
			}
			ViewShake( DeltaTime );
		}
	}

	function ProcessMove(float DeltaTime, vector NewAccel, eDoubleClickDir DoubleClickMove, rotator DeltaRot)
	{
		if( Pawn == None )
		{
			return;
		}

		if (Role == ROLE_Authority)
		{
			// Update ViewPitch for remote clients
			Pawn.SetRemoteViewPitch( Rotation.Pitch );
		}

		Pawn.Acceleration = NewAccel;

		CheckJumpOrDuck();
	}

	function PlayerMove( float DeltaTime )
	{
		local vector			NewAccel;
		local eDoubleClickDir	DoubleClickMove;
		local rotator			OldRotation;
		local bool				bSaveJump;

		if( Pawn == None )
		{
			GotoState('Dead');
		}
		else
		{
			NewAccel.X = PlayerInput.aForward;
			NewAccel.Y = PlayerInput.aStrafe;
			NewAccel.Z = 0;
			NewAccel = Pawn.AccelRate * Normal(NewAccel);

			if (IsLocalPlayerController())
			{
				AdjustPlayerWalkingMoveAccel(NewAccel);
			}

			DoubleClickMove = PlayerInput.CheckForDoubleClickMove( DeltaTime/WorldInfo.TimeDilation );

			// Update rotation.
			OldRotation = Rotation;
			UpdateRotation( DeltaTime );
			bDoubleJump = false;

			if( bPressedJump && Pawn.CannotJumpNow() )
			{
				bSaveJump = true;
				bPressedJump = false;
			}
			else
			{
				bSaveJump = false;
			}

			if( Role < ROLE_Authority ) // then save this move and replicate it
			{
				ReplicateMove(DeltaTime, NewAccel, DoubleClickMove, OldRotation - Rotation);
			}
			else
			{
				ProcessMove(DeltaTime, NewAccel, DoubleClickMove, OldRotation - Rotation);
			}
			bPressedJump = bSaveJump;
		}
	}

Begin:
}


DefaultProperties
{
	CameraClass = class'Ace.AcePlayerCamera'
	InputClass = class'Ace.AcePlayerInput'
}
